package com.kylin.test.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;

import com.kylin.test.entity.ContactsEntity;

import java.util.ArrayList;
import android.provider.ContactsContract.CommonDataKinds.Phone;

/**
 * Created by kylinhuang on 21/02/2017.
 */

public class ContactsUtils {

    /**
     * 获取手机通讯录联系人
     */
    public static ArrayList<ContactsEntity>  getAllContacts(Context mContext) {

        ArrayList<ContactsEntity>  list = new ArrayList<ContactsEntity>();
        ContactsEntity contactsEntity ;

        ContentResolver cr = mContext.getContentResolver();
        Cursor cursor = cr.query(
                Phone.CONTENT_URI,
                null,
                null,
                null,
                null);

        while (cursor.moveToNext()) {
            contactsEntity = new ContactsEntity();


            //读取通讯录的姓名
            String name = cursor.getString(cursor
                    .getColumnIndex(Phone.DISPLAY_NAME));
            //读取通讯录的号码
            String number = cursor.getString(cursor
                    .getColumnIndex(Phone.NUMBER));


            //当手机号码为空的或者为空字段 跳过当前循环
            if (TextUtils.isEmpty(number))  continue;
            contactsEntity.setPhoneNumber(number);

            Log.e("Contacts ","name " + name + " number " +number  );
            list.add(contactsEntity);
        }
        cursor.close();


        return list;

    }

}
